﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Modules.ACTP.Components;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEditEmployee : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditEmployee_Update;
            this.Delete += AddEditEmployee_Delete;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                btnAssignCertification.Visible = this.ItemID > 0;

                if (this.ItemID > 0)
                {
                    Components.Employee employee = Components.Employee.Load(this.ItemID);
                    if (employee != null)
                    {
                        // Membership.
                        User user = User.Load(employee.MembershipID);
                        txtEmail.Text = user.Email;
                        txtUsername.Text = user.Username;

                        txtEmployeeCode.Text = employee.EmployeeCode;
                        txtFirstName.Text = employee.FirstName;
                        txtLastName.Text = employee.LastName;
                        txtPersonalTitle.Text = employee.PersonalTitle;
                        txtJobTitle.Text = employee.JobTitle;
                        rbGender.Checked = employee.Gender;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        void AddEditEmployee_Update(object sender, EventArgs e)
        {
            User user = new User();
            Components.Employee employee = new Components.Employee();
            if (this.ItemID > 0)
            {
                employee = Components.Employee.Load(this.ItemID);
                user = User.Load(employee.MembershipID);
            }

           
            user.PortalID = Global.PortalID;
            user.Email = txtEmail.Text;
            user.Username = txtUsername.Text;

            employee.ModuleID = this.ModuleID;
            employee.EmployeeCode = txtEmployeeCode.Text;
            employee.FirstName = txtFirstName.Text;
            employee.LastName = txtLastName.Text;
            employee.PersonalTitle = txtPersonalTitle.Text;
            employee.JobTitle = txtJobTitle.Text;
            employee.Gender = Convert.ToBoolean(rbGender.Checked);

            // Insert.
            if (this.ItemID == 0)
            {
                user.Name = employee.FirstName + " " + employee.LastName;
                // Default password: p@ssw0rd.
                user.Password = PortalSecurity.Encrypt(txtPassword.Text.Trim());
                user.Status = "ACTIVE";

                // Check exists email / username?
                if (User.CheckExist(txtEmail.Text.Trim()))
                {
                    lblMessage.Text = "Selected email is exists.";
                    return;
                }
                if (User.CheckUsernameExist(txtUsername.Text.Trim()))
                {
                    lblMessage.Text = "Selected username is exists.";
                    return;
                }

                // Insert into membership.
                int membershipID = user.Insert();

                employee.MembershipID = membershipID;
                employee.CreatedBy = employee.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                employee.CreatedDate = employee.UpdatedDate = DateTime.Now;
                this.ItemID = employee.Insert();
            }
            // Update.
            else
            {
                if (txtPassword.Text.Length > 0)
                {
                    user.Password = PortalSecurity.Encrypt(txtPassword.Text.Trim());
                }

                user.Update();

                employee.EmployeeID = this.ItemID;
                employee.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
                employee.UpdatedDate = DateTime.Now;
                employee.Update();
            }

            // Go to list.
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.EmployeeList);
            Response.Redirect(url);

        }

        void AddEditEmployee_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Employee employee = Employee.Load(this.ItemID);
                if (employee != null)
                {
                    // Delete employee.
                    Employee.DeleteEmployee(this.ItemID);
                    
                    // Delete membership.
                    User user = User.Load(employee.MembershipID);
                    User.DeleteUser(user.ID);

                    // Go back.
                    this.ReturnToReferrerPage();
                }
            }
        }

        protected void btnAssignCertification_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AssignCertificationToEmployee", "employeeid={0}", this.ItemID);
            Response.Redirect(url);
        }
    }
}
